import axios from 'axios';

const server = axios.create({
  baseURL: '', // 请求url
  timeout: 3000, // 超时处理
});

server.interceptors.request.use((config)=>{
    let token = localStorage.getItem("auth-token")
    if(token){
      config.headers["auth-token"] = token
    }else{
      config.headers["auth-token"] = null
    }
    return config;
  },(error)=>{
    return error;
  }
);
server.interceptors.response.use(res=>{
  if(res.data.code==400){
    ElMessage.error(res.data.message)
  }
  return res.data
},error=>{
  switch (error && error.response && error.response.status) {
    case 400:
      error.message = '请求错误';
      break;
    case 401:
      error.message = '未授权，请登录';
      break;
  }
  return Promise.reject(error);
})

export default server;